#!/usr/bin/ruby
#-*- coding: UTF-8 -*-
require 'xcodeproj'
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

# _xcodeProjPath = "/Users/admin/Documents/workspace/b2/release/0.4.1/frameworks/runtime-src/proj.ios_appstore_yuntiankong_iosappstore_nzhc/arpg.xcodeproj"#ARGV[0]
# _originPlatform = "yuntiankong_iosappstore_nzhc"#ARGV[1]
# _workPlatform = "yuntiankong_iosappstore_nzhc"#ARGV[2]
# _bundleID = "com.new.bundle.id"#ARGV[3]
# _gameVersion = "1992.10.06"#ARGV[4]
# _gameChineseName = "我的第一个游戏"#ARGV[5]
_xcodeProjPath = ARGV[0]
_scriptPath = ARGV[1]
_cachePath = ARGV[2]

platform_project = Xcodeproj::Project.open(_xcodeProjPath)
platform_target = platform_project.targets[0]

# #获得build phases
platform_target.build_phases.each do |sub_file|
#    puts sub_file.class
	if "#{sub_file.class}" == "Xcodeproj::Project::Object::PBXShellScriptBuildPhase"
		if !_scriptPath.empty? or !_cachePath.empty?
			run_script_file = sub_file
			#puts run_script_file.shell_script
			puts "start to config run script"
			script = run_script_file.shell_script
        	script_command_array = script.split("\n")
#        	puts script_array.size
#        	puts "#{array}"
			if script_command_array.size > 0
				last_script_str = script_command_array.last
        		modify_script_array = last_script_str.split(" ")
        		# puts modify_script_array.size
        		# puts "#{modify_script_array}"
        		# puts "#{modify_script_array[4]}"

				if modify_script_array.size >= 9
					isPythonScript = 0
        			if modify_script_array[0] == "python"
        				isPythonScript = 1
        			end
					if !_scriptPath.empty?
						modify_script_array[0 + isPythonScript] = _scriptPath
					end
					if !_cachePath.empty?
						modify_script_array[3 + isPythonScript] = "\"#{_cachePath}\""
					end
				end
        		
        		last_script_str = modify_script_array.join(" ")
        		script_command_array.pop
        		script_command_array.push(last_script_str)
        		script = script_command_array.join("\n")
        		# puts script
#        		puts script
				run_script_file.shell_script = script
				puts "end config run script"
        		puts "\n"
			end
		end
	end
end
puts "save modify"
platform_project.save

